// 新增页面子表编辑前事件

import deepCopy from '../../../../../public/functions/deep-copy';
import beforeEditTable from '../request/beforeEditTable';


export default async function(props, tableId, key, value, index, record, status, func_type = 1) {
    const {
        pageFrom,
        cardTable: {
            getAllRows,
            setEditableByIndex
        },
        humanResourceOrganizationValue: {
            refpk
        },
        meta: {
            getMeta,
            setMeta
        }
    } = this.props;

    let allTableData = getAllRows(tableId);
    let template = getMeta();

    allTableData.forEach((item) => {
        let values = {};
        Object.keys(item.values).forEach((key) => {
            let value = {};
            Object.keys(item.values[key]).forEach((valueKey) => {   
                if(valueKey !== 'scale') {
                    value[valueKey] = item.values[key][valueKey];
                }
            });
            values[key] = value;
        });
        item.values = values
    });

    let postData = {
        areaCode: tableId,
        editType: pageFrom === 'add' ? 1 : 2,
        func_type: func_type,
        key: key, 
        model: {
            areacode: tableId,
            rows: allTableData
        },
        pk_org: refpk,
        rowCount: allTableData.length,
        rowIndex: index
    };

    try {
        let res = await beforeEditTable(postData);
        let editTable = res.data.editable;
        let param = res.data.refParam;

        setEditableByIndex(tableId, index, key, editTable);

        // if(param) {
        //     template[tableId].items.forEach((item) => {
        //         if(item.attrcode === key) {
        //             item['queryCondition'] = param;
        //         }
        //     });
        // }
        template[tableId].items.map((item) => {
            if(param && item.attrcode === key) {
                item['queryCondition'] = param;
            } 
            else if (item.attrcode === key && item.itemtype === 'refer') {
                if (!item.queryCondition) item.queryCondition = {}
                Object.assign(item.queryCondition, {
                    pk_org: refpk
                })
            }
        });
        setMeta(template);
    }
    catch(e) {
    }

    return true;
}
